<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
abstract class LM_Ranking_Abstract
{
    protected $rows;
    protected $project;
    protected $results;
    protected $teams;

    public function __construct(LM_Table_Project $project)
    {
        $this->results = array();

        $this->project = $project;
        $this->teams = $project->getTeams();

        $excludes = $project->getParam("excludes", array());

        $this->rows = array();
        foreach ($this->teams AS $team) {
            if (!in_array($team->team_id, $excludes)) {
                $this->rows[$team->team_id] = new Leaguetablerow($team);
            }
        }
    }

    public function setResults($results = array())
    {
        $this->results = $results;
    }

    public function getTable()
    {
        foreach ($this->results AS $result) {

            $row1 = $this->getRow($result->team1_id);
            $row2 = $this->getRow($result->team2_id);

            if ($row1 != null && $row2 != null) {
                $this->updateRow($row1, $result->e1, $result->e2, $result->type);
                $this->updateRow($row2, $result->e2, $result->e1, $result->type);
            }
        }

        uasort($this->rows, array($this, "compare"));
        return $this->rows;
    }

    abstract public function compare(Leaguetablerow $a, Leaguetablerow $b);

    public function getRow($teamid)
    {
        if (isset($this->rows[$teamid])) {
            return $this->rows[$teamid];
        } else {
            return null;
        }
    }

    public function updateRow($row, $e1, $e2, $type = 0)
    {

        if ($e1 > $e2) {
            switch ($type) {
                case 0:
                    $row->incV1();
                    $points = $this->project->getParam("points_after_regular_time_v");
                    $points2 = $this->project->getParam("points_after_regular_time_d");
                    break;

                case 1:
                    $row->incV2();
                    $points = $this->project->getParam("points_after_extra_time_v");
                    $points2 = $this->project->getParam("points_after_extra_time_d");
                    break;

                case 2:
                    $row->incV3();
                    $points = $this->project->getParam("points_after_penalty_v");
                    $points2 = $this->project->getParam("points_after_penalty_d");
                    break;
            }
        }

        if ($e1 == $e2) {
            switch ($type) {
                case 0:
                    $row->incT1();
                    $points = $this->project->getParam("points_after_regular_time_t");
                    $points2 = $this->project->getParam("points_after_regular_time_t");
                    break;

                case 1:
                    $row->incT2();
                    $points = $this->project->getParam("points_after_extra_time_t");
                    $points2 = $this->project->getParam("points_after_extra_time_t");
                    break;

                case 2:
                    $row->incT3();
                    $points = $this->project->getParam("points_after_penalty_t");
                    $points2 = $this->project->getParam("points_after_penalty_t");
                    break;
            }
        }

        if ($e1 < $e2) {
            switch ($type) {
                case 0:
                    $row->incD1();
                    $points = $this->project->getParam("points_after_regular_time_d");
                    $points2 = $this->project->getParam("points_after_regular_time_v");
                    break;

                case 1:
                    $row->incD2();
                    $points = $this->project->getParam("points_after_extra_time_d");
                    $points2 = $this->project->getParam("points_after_extra_time_v");
                    break;

                case 2:
                    $row->incD3();
                    $points = $this->project->getParam("points_after_penalty_d");
                    $points2 = $this->project->getParam("points_after_penalty_v");
                    break;
            }
        }

        $row->incMatches();
        $row->addScore1($e1);
        $row->addScore2($e2);
        $row->addPoints($points);
        $row->addPoints2($points2);
    }

}